# -*- coding: utf-8 -*-

def index():
    return dict(
        )

@check_session
def the_shop():
    user_name = session.user_name
    shop = sip.taobao.shop.get(fields=['title'], nick=user_name)
    onsale = sip.taobao.items.onsale.get(fields=['title','num','pic_path','price',])
    return dict(
        user = user_name,
        shop_name = shop['rsp']['shops'][0]['title'],
        onsale = onsale,
        )

def interface():
    each_api = [i.api for i in db().select(db.interface.ALL)]
    form = FORM(TABLE(
        TR("API:",SELECT(each_api, _name='api', requires=IS_IN_SET(each_api))),
        TR("",INPUT(_type="submit",_value="SUBMIT")),
        ))
    if form.accepts(request.vars,session):
        response.flash="form accepted"
    elif form.errors:
        response.flash="form is invalid"

    try:
        api_name = form.vars.api
    except:
        api_name = None
    table = db(db.interface.api==api_name).select()

    return dict(
        table = table,
        each_api = each_api,
        form = form,
        )
@auth.requires_login()
def images():
    images = db(db.images.posted_by == auth.user.id).select(db.images.ALL)
    return dict(
        images = images
        )

def download():
    return response.download(request, db)

def user(): return dict(form=auth())
